package com.faradel.pharmapp.managedbean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.faradel.pharmapp.entities.Patient;
import org.faradel.pharmapp.entities.PatientOrder;
import org.springframework.context.ApplicationContext;
import org.springframework.web.jsf.FacesContextUtils;

import com.faradel.pharmapp.service.PatientServiceInterface;

@ManagedBean
@ViewScoped
public class PatientBean implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Resource
    private PatientOrder patientOrder;

    @Resource
    private Patient patient;

    private String patientId;

    private List<Patient> patientList;

    private Map<String, String> searchMap = new HashMap<String, String>();

    private String action;

    private String firstName;
    private String lastName;
    private Date dateOfBirth;

    private Date maxDate;

    /**
     * Initialize
     */
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        patient = (Patient) context.getBean("patient");
        patientOrder = (PatientOrder) context.getBean("patientOrder");

        Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        patientList = new ArrayList<Patient>();

        maxDate = TimeClass.getCurrentDate();
        action = params.get("action");
    }

    public String createPrescription()
    {
        action = "create";
        String s = "processprescription";
        return s;
    }

    private PatientServiceInterface getPatientServiceInterface()
    {
        ApplicationContext context = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance());
        return (PatientServiceInterface) context.getBean("patientServiceImpl");
    }

    /**
     * Find patient using firstname last name and date of birth
     */
    public void searchPatient()
    {
        searchMap.put("firstName", firstName);
        searchMap.put("lastName", lastName);

        DateFormat df = new SimpleDateFormat("y-MM-d");
        searchMap.put("dob", df.format(dateOfBirth));

        if (patientList.size() > 0)
            patientList.clear();
        patientList = getPatientServiceInterface().getPatientByColumns(searchMap);
    }

    public String update()
    {
        action = "update";
        String s = "processpatient";
        return s;
    }

    public String delete()
    {
        action = "delete";
        String s = "processpatient";
        return s;
    }

    public String view()
    {
        action = "find";
        String s = "processpatient";
        return s;
    }

    public void findPatientById()
    {
        patient = getPatientServiceInterface().getPatientById(Integer.parseInt(patientId));
        if (patientList.size() > 0)
            patientList.clear();
        patientList.add(patient);
    }

    public PatientOrder getPatientOrder()
    {
        return patientOrder;
    }

    public void setPatientOrder(PatientOrder patientOrder)
    {
        this.patientOrder = patientOrder;
    }

    public Patient getPatient()
    {
        return patient;
    }

    public void setPatient(Patient patient)
    {
        this.patient = patient;
    }

    public List<Patient> getPatientList()
    {
        return patientList;
    }

    public void setPatientList(List<Patient> patientList)
    {
        this.patientList = patientList;
    }

    public Map<String, String> getSearchMap()
    {
        return searchMap;
    }

    public void setSearchMap(Map<String, String> searchMap)
    {
        this.searchMap = searchMap;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getPatientId()
    {
        return patientId;
    }

    public void setPatientId(String patientId)
    {
        this.patientId = patientId;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public Date getDateOfBirth()
    {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth)
    {
        this.dateOfBirth = dateOfBirth;
    }

    public Date getMaxDate()
    {
        return maxDate;
    }

    public void setMaxDate(Date maxDate)
    {
        this.maxDate = maxDate;
    }

}
